﻿using SetSailDal;
using SetSailModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SetSailView
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Frm_ZDGLAdd : Form
    {
        public Frm_ZDGLAdd()
        {
            InitializeComponent();
            _zdgl = new QH_ZDGL();
            id = "";
        }
        public string id { get; set; }
        public QH_ZDGL _zdgl { get; set; }
        public Frm_ZDGLAdd(string id)
        {
            InitializeComponent();
            this.id = id;
            _zdgl = new QH_ZDGL();
        }


        //window标准拖拽事件
        [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
        public static extern void ReleaseCapture();
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern void SendMessage(int hwnd, int wMsg, int wParam, int lParam);

        private void Frm_ZDGLAdd_Load(object sender, EventArgs e)
        {
            this.dgv_zdmx.AutoGenerateColumns = false;
            BindingSource bs = new BindingSource();

            //获取字典
            _zdgl = ZDGLDal.GetModel(id);
            if (_zdgl.ZDDM != null)
            {
                txt_ZDDM.InputText = _zdgl.ZDDM.ToString();
                txt_ZDMC.InputText = _zdgl.ZDMC.ToString();
            }

            //获取字段明细
            ResultModel<List<QH_ZDMX>> result = ZDGLDal.GetZDMXList($" ZDID = '{this.id}'");
            if (result.Code == ResultCode.Success)
            {
                if (result.Data.Count > 0)
                {
                    bs = new BindingSource();
                    bs.DataSource = result.Data;
                    this.dgv_zdmx.DataSource = bs;
                }
                else
                {
                    QH_ZDMX zdmx = new QH_ZDMX();
                    //zdmx.btn = "删除";
                    bs.DataSource = new List<QH_ZDMX>() { zdmx };
                    this.dgv_zdmx.DataSource = bs;
                }
            }
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage((int)this.Handle, 0xA1, 2, 0);
                //Console.WriteLine(ActionServo1.Location.X+"--"+ ActionServo1.Location.Y);
            }
        }

        private void ucBtnExt4_BtnClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ucBtnExt1_BtnClick(object sender, EventArgs e)
        {
            //释放鼠标焦点
            this.Validate();

            var txtzdmc = txt_ZDMC.InputText.ToString();
            var txtzddm = txt_ZDDM.InputText.ToString();
            BindingSource bs = new BindingSource();
            var bx = dgv_zdmx.DataSource as BindingSource;
            List<QH_ZDMX> zdmxList = bx.DataSource as List<QH_ZDMX>;
            ResultModel<int> result;
            string yanzheng = $"ZDDM='{txtzddm.Trim()}'";
            if (this.id.Length > 0) //编辑
                yanzheng += $" AND  ID <> '{this.id}'";
            int count = ZDGLDal.GetCount(yanzheng);
            if (count > 0)
            {
                MessShow.ShowHint("字典代码重复");
                return;
            }
            if (this.id.Length > 0)
            {
                //编辑
                _zdgl.ZDMC = txtzdmc;
                _zdgl.ZDDM = txtzddm;
                result = ZDGLDal.EditZDGL(_zdgl, zdmxList.Where(obj => obj.MXDM != null && obj.MXDM.Length > 0).ToList());
            }
            else
            {
                QH_ZDGL _zdgl = new QH_ZDGL();
                _zdgl.ZDMC = txtzdmc;
                _zdgl.ZDDM = txtzddm;
                string ID = SetSailCommon.CommonHelper.GetNewGuid(); // 先获取赋值id，方便明细新增
                _zdgl.ID = ID;
                result = SetSailDal.ZDGLDal.SaveZDGL(_zdgl, zdmxList.Where(obj => obj.MXDM != null && obj.MXDM.Length > 0).ToList());
            }
            if (result.Code == ResultCode.Success)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessShow.ShowHint(result.Message);
            }
        }
    }
}
